/**
 * instantiate a modal popup dialog with form included
 *
 * @param string formcontainerid
 * @param string xhr target, e.g. ?test.php
 * @param string width of container, default: 200px
 * @param bool keep dialog in center, default = true
 * @param array btn definiton 0 = okay btn text, 1 = cancel btn text
 */
function phpmyPopUpModalForm (formcontainerid, posttarget, vwidth, vfixedcenter, btn) {
    if (!vwidth)
        vwidth = null;
    if (!vfixedcenter)
        vfixedcenter = true;
    if (btn) {
        if (!btn[0])
            btnokay = 'okay';
        else
            btnokay = btn[0];
        if (!btn[1])
            btncancel = 'cancel';
        else
            btncancel = btn[1];
    } else {
        btncancel = 'cancel';
        btnokay = 'okay';
    }
    // Define various event handlers for Dialog
    var handleSubmit = function(a,b,c,d) {
        // check if textarea is inside, supress submit on enter
        var tst = YAHOO.util.Dom.getElementsBy(function(el){return true;},'textarea', formcontainerid);
        if (tst[0]) {
            if (b[0] == 13) {
                return;
            }
        }
        // send form
        // get form id within container
        var myEls = YAHOO.util.Dom.getElementsBy(function(el){return true;},'form', formcontainerid);
        myEls = YAHOO.util.Dom.get(myEls[0]);
        _ajaxSendPostForm (posttarget, myEls.getAttribute("id"));
    };
    var handleCancel = function() {
        this.hide();
    };
    // Instantiate the Dialog
    var anntype = new YAHOO.widget.Dialog(formcontainerid,
                            { width : vwidth,
                              fixedcenter : vfixedcenter,
                              visible : false,
                              modal: true,
                              zindex: 1000,
                              postmethod: "none",
                              constraintoviewport : true,
                              buttons : [{ text:btnokay, handler:handleSubmit, isDefault:true },
                                         { text:btncancel, handler:handleCancel } ]
                            });
    var kl = new YAHOO.util.KeyListener(formcontainerid, { keys:13 },
            { fn:handleSubmit,
              scope:anntype,
              correctScope:true }
              );
	var kl2 = new YAHOO.util.KeyListener(formcontainerid, { keys:27 },
            { fn:handleCancel,
              scope:anntype,
              correctScope:true }
              );
	anntype.cfg.queueProperty("keylisteners", [kl, kl2]);
	var resize = function () {
        var el = YAHOO.util.Dom.get(formcontainerid + '_c');
        var elHeight = el.offsetHeight > YAHOO.util.Dom.getViewportHeight() ? YAHOO.util.Dom.getViewportHeight() - 20 + 'px' : false;
        if(elHeight){
            // move to upper screen
            anntype.cfg.queueProperty("y", 10);
            anntype.cfg.queueProperty("height", elHeight);
        } else {
            anntype.cfg.queueProperty("height", null);
        }
        anntype.render(document.body);
    };
    anntype.beforeShowEvent.subscribe(resize);
    anntype.changeContentEvent.subscribe(resize);
    resize();
    document.body.onkeypress = stopReturnKey;
    return anntype;
}
/**
 * init modal content box
 * @param string header
 * @param string body
 * @param bool init visible or not
 * @return object instance
 */
function phpmyContentModal (header, body, visible)
{
 var mask =  new YAHOO.widget.Panel("wait",
         { width:"240px",
            fixedcenter:true,
            close:false,
            draggable:false,
            zindex:1002,
            modal:true,
            visible:(visible ? true : false )}
 );
 mask.setHeader(header);
 mask.setBody(body);
 mask.render(document.body);
 return mask;
}
 /**
  * instantiate a simple modal popup dialog with form included and one button for submit
  *
  * @param string formcontainerid
  * @param string xhr target, e.g. ?test.php
  * @param string width of container, default: 200px
  * @param bool keep dialog in center, default = true
  * @param string btn definition
  */
function phpmyPopUpSimpleModalForm (formcontainerid, posttarget, vwidth, vfixedcenter, btn) {
     if (!vwidth)
         vwidth = '200px';
     if (!vfixedcenter)
         vfixedcenter = true;
     if (btn) {
         btnokay = btn;
     } else {
         btnokay = 'okay';
     }
     // Define various event handlers for Dialog
     var handleSubmit = function() {
         // send form
         // get form id within container
         var myEls = YAHOO.util.Dom.getElementsBy(function(el){return true;},'form', formcontainerid);
         myEls = YAHOO.util.Dom.get(myEls[0]);
         _ajaxSendPostForm (posttarget, myEls.getAttribute("id"));
     };
     var handleCancel = function() {
         this.cancel();
     };
     // Instantiate the Dialog
     var anntype = new YAHOO.widget.Dialog(formcontainerid,
                             { width : vwidth,
                               fixedcenter : vfixedcenter,
                               visible : false,
                               modal: true,
                               zindex: 1000,
                               postmethod: "form",
                               close: false,
                               draggable: false,
                               constraintoviewport : true,
                               buttons : [{ text:btnokay, handler:handleSubmit, isDefault:true }]
                             });
     var kl = new YAHOO.util.KeyListener(formcontainerid, { keys:13 },
                                               { fn:handleSubmit,
                                                 scope:anntype,
                                                 correctScope:true }
                                                 );
     anntype.cfg.queueProperty("keylisteners", kl);
     anntype.render(document.body);
     var el = YAHOO.util.Dom.get(formcontainerid + '_c');
     var elHeight = el.offsetHeight > YAHOO.util.Dom.getViewportHeight() ? YAHOO.util.Dom.getViewportHeight() - 20 + 'px' : '';
     if(elHeight){
         anntype.cfg.queueProperty("height", elHeight);
         anntype.render(document.body);
     }
     document.body.onkeypress = stopReturnKey;
     return anntype;
 }

/**
 * instantiate a simple modal popup dialog with form included and one button for submit
 *
 * @param string formcontainerid
 * @param string xhr target, e.g. ?test.php
 * @param string width of container, default: 200px
 * @param bool keep dialog in center, default = true
 * @param string btn definition
 */
function phpmyPopUpSimpleNonModalForm (formcontainerid, posttarget, vwidth, vfixedcenter, btn) {
    if (!vwidth)
        vwidth = null;
    if (!vfixedcenter)
        vfixedcenter = true;
    if (typeof btn == 'string') {
        btnokay = btn;
    } else {
        btnokay = 'okay';
    }
    // Define various event handlers for Dialog
    var handleSubmit = function() {
        // send form
        // get form id within container
        var myEls = YAHOO.util.Dom.getElementsBy(function(el){return true;},'form', formcontainerid);
        myEls = YAHOO.util.Dom.get(myEls[0]);
        if (myEls.getAttribute("id")) _ajaxSendPostForm (posttarget, myEls.getAttribute("id"));
    };
    var handleCancel = function() {
    	this.cancel();
    };
    // Instantiate the Dialog
    var anntype = new YAHOO.widget.Dialog(formcontainerid,
                            { width : vwidth,
                              fixedcenter : vfixedcenter,
                              visible : false,
                              modal: false,
                              zindex: 1000,
                              postmethod: "form",
                              close: true,
                              draggable: true,
                              constraintoviewport : true,
                              buttons : btnokay ? [{ text:btnokay, handler:handleSubmit, isDefault:true }] : []
                            });
    var kl = new YAHOO.util.KeyListener(formcontainerid, { keys:13 },
            { fn:handleSubmit,
              scope:anntype,
              correctScope:true }
              );
	var kl2 = new YAHOO.util.KeyListener(formcontainerid, { keys:27 },
            { fn:handleCancel,
              scope:anntype,
              correctScope:true }
              );
	anntype.cfg.queueProperty("keylisteners", [kl, kl2]);
    var resize = function () {
        var el = YAHOO.util.Dom.get(formcontainerid + '_c');
        var elHeight = el.offsetHeight+10 > YAHOO.util.Dom.getViewportHeight() ? YAHOO.util.Dom.getViewportHeight() - 20 + 'px' : false;
        if(elHeight){
            anntype.cfg.queueProperty("height", elHeight);
            anntype.render(document.body);
        } else {
            anntype.cfg.queueProperty("height", null);
            anntype.render(document.body);
        }
    };
    anntype.beforeShowEvent.subscribe(resize);
    anntype.render(document.body);
    resize();
    document.body.onkeypress = stopReturnKey;
    return anntype;
}
//prevent enter from entire page - user keylistener to receive enter
function stopReturnKey(evt) {
  var evt = (evt) ? evt : ((event) ? event : null);
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;}
}
/**
 * init modal content box
 * @param string body
 * @param string header
 * @return object instance
 */
function phpmyAlertReplacement (body, header)
{
    if (!header) header = "Information";
    var mask =  new YAHOO.widget.SimpleDialog("alertReplacement",
         {  icon: YAHOO.widget.SimpleDialog.ICON_WARN,
            fixedcenter:true,
            close:true,
            draggable:true,
            zindex:1002,
            modal:true,
            visible:true,
            buttons: [ { text:"Okay", handler: function(){this.hide();}, isDefault:true }]
         }
    );
    mask.setHeader(header);
    mask.setBody(body);
    mask.render(document.body);
    return mask;
}
//window.alert = function (text){
//    phpmyAlertReplacement(text);
//}